//
//  AppDelegate.m
//  Delicious2
//
//  Created by FSD on 16/1/6.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "AppDelegate.h"
#import "UMSocial.h"            //友盟头文件
#import "XZMCoreNewFeatureVC.h" //引导页视频头文件
#import "CALayer+Transition.h"  //视频动画

@interface AppDelegate ()

@property (nonatomic, strong) UITabBarController *tabBarVC;

@end

@implementation AppDelegate

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    //打开传感器
    [UIDevice currentDevice].proximityMonitoringEnabled = YES;
    
    //友盟appKey
    [UMSocialData setAppKey:@"507fcab25270157b37000010"];
    
    
    // 要使用百度地图，请先启动BaiduMapManager
    BMKMapManager *mapManager = [[BMKMapManager alloc]init];
    // 如果要关注网络及授权验证事件，请设定     generalDelegate参数
    BOOL ret = [mapManager start:@"8XxHbc2PTMj1Cv84ipFbm3FQ"  generalDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed!");
    } else {
        NSLog(@"success");
    }
    
    
    //食谱
    CookBookViewController *cookBookVC=[[CookBookViewController alloc]init];
    UINavigationController *cookBookNAV = [[UINavigationController alloc]initWithRootViewController:cookBookVC];
    cookBookVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"食谱" image:[UIImage imageNamed:@"ImageCookBook"]  selectedImage:[UIImage imageNamed:@"ImageCookBook_unSelect"]];
    
    //提示框
    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:cookBookVC.view animated:YES];
    HUD.mode = MBProgressHUDModeText;
    HUD.yOffset = HEIGHT / 3;
    HUD.margin = 5;
    HUD.cornerRadius = 5;
    
    //    判断网络状态
    Reachability *reach = [Reachability reachabilityWithHostname:@"www.apple.com"];
    if (reach.isReachable) {
        if (reach.isReachableViaWiFi) {
            HUD.labelText = @"当前WIFI连接";
            [HUD hide:YES afterDelay:3];
        } else if (reach.isReachableViaWWAN) {
            HUD.labelText = @"当前移动网络连接";
            [HUD hide:YES afterDelay:3];
        }
    } else {
        HUD.labelText = @"当前无网络连接";
        [HUD hide:YES afterDelay:1];
    }

    //吃
    EatViewController *eatVC=[[EatViewController alloc]init];
    UINavigationController *eatNAV = [[UINavigationController alloc]initWithRootViewController:eatVC];
    eatVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"吃" image:[UIImage imageNamed:@"ImageEat"]  selectedImage:[UIImage imageNamed:@"ImageEat_unSelect"]];
    
    //食课
    VideoViewController *videoVC=[[VideoViewController alloc]init];
    UINavigationController *videoNAV = [[UINavigationController alloc]initWithRootViewController:videoVC];
    videoVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"食课" image:[UIImage imageNamed:@"ImageVideo"]  selectedImage:[UIImage imageNamed:@"ImageVideo_unSelect"]];
    
    //我的
    MineViewController *mineVC=[[MineViewController alloc]init];
    UINavigationController *mineNAV = [[UINavigationController alloc]initWithRootViewController:mineVC];
    mineVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"ImageMine"]  selectedImage:[UIImage imageNamed:@"ImageMine_unSelect"]];
    
    //抽屉弹出左侧VC
    LeftViewController *left = [[LeftViewController alloc]init];
    
    
    self.tabBarVC = [[UITabBarController alloc] init];
    self.tabBarVC.tabBar.tintColor = kColor;
    self.tabBarVC.viewControllers = @[cookBookNAV, eatNAV, videoNAV, mineNAV];
    
    //左侧抽屉
    self.drawerVC = [[DrawerViewController alloc] init];
    self.drawerVC.rootViewController = self.tabBarVC;
    self.drawerVC.leftViewController = left;
    self.drawerVC.leftViewShowWidth = 250;
    self.drawerVC.needSwipeShowMenu = true;//默认开启的可滑动展示

    //改变时间栏颜色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    //引导页视频
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"YES"]) {
        [[NSUserDefaults standardUserDefaults] setObject:@"Cja" forKey:@"YES"];

        self.window.rootViewController = [XZMCoreNewFeatureVC newFeatureVCWithPlayerURL:[[NSBundle mainBundle] URLForResource:@"2.m4v" withExtension:nil] enterBlock:^{
            self.window.rootViewController = self.drawerVC;
            //翻页动画
            [self.window.layer transitionWithAnimType:TransitionAnimTypeRippleEffect subType:TransitionSubtypesFromRamdom curve:TransitionCurveRamdom duration:1.0f];
            
        } configuration:^(AVPlayerLayer *playerLayer) {

        }];
        
    } else {
        self.window.rootViewController = self.drawerVC;
    }
    
    [self.window makeKeyAndVisible];
    
    //进入判断是否夜间模式发送通知
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(receiveNotification:) name:@"changeColor" object:nil];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"NightMode"];
    if ([passWord isEqualToString:@"Night"]) {
        self.tabBarVC.tabBar.barTintColor = [UIColor grayColor];
    } else {
        self.tabBarVC.tabBar.barTintColor = [UIColor whiteColor];
    }
    
    return YES;
}

#pragma mark ----- 夜间模式改变颜色方法
-(void)receiveNotification:(NSNotification *)sender {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *mode = [user objectForKey:@"NightMode"];
    if ([mode isEqualToString:@"Night"]) {
        self.tabBarVC.tabBar.barTintColor = [UIColor grayColor];
    } else {
        self.tabBarVC.tabBar.barTintColor = [UIColor whiteColor];
    }
}

//百度地图授权
- (void)onGetNetworkState:(int)iError {
    if (0 == iError) {
        NSLog(@"联网成功");
    }
    else{
        NSLog(@"onGetNetworkState %d",iError);
    }
    
}

- (void)onGetPermissionState:(int)iError {
    if (0 == iError) {
        NSLog(@"授权成功");
    }
    else {
        NSLog(@"onGetPermissionState %d",iError);
    }
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    
    [BMKMapView didForeGround];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
